#include "qwtdialhost.h"

#include "../../libs/shared/qhostfactory.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/qwt/qwt_dial.h"
#include "../../libs/qwt/qwt_dial_needle.h"

QDialWidget::QDialWidget(QWidget *parent):
    QwtDial(parent)
{
    QwtDialSimpleNeedle *needle = new QwtDialSimpleNeedle(
                QwtDialSimpleNeedle::Arrow, true, QColor(Qt::red),
        QColor( Qt::gray ).light( 130 ) );
    setNeedle( needle );
    setFocusPolicy(Qt::StrongFocus);
    m_needle_info.m_color.setColorAt(0,QColor(Qt::red));
    m_needle_info.m_knob_color.setColorAt(0,QColor(Qt::gray).light(130));
}

void QDialWidget::setNeedleInfo(const tagNeedleInfo &info)
{
    if(m_needle_info!=info)
    {
        m_needle_info=info;
        QwtDialSimpleNeedle *nee=(QwtDialSimpleNeedle*)needle();
        nee->setWidth(info.m_width);
        nee->setStyle((QwtDialSimpleNeedle::Style)info.m_style);
        QPalette p=nee->palette();
        p.setColor(QPalette::Mid,info.m_color.stops().first().second);
        p.setColor(QPalette::Base,info.m_knob_color.stops().first().second);
        nee->setPalette(p);
        nee->setHasKnob(info.m_knob_visible);
        update();
    }
}

tagNeedleInfo QDialWidget::needleInfo()
{
    return m_needle_info;
}

QwtDialHost::QwtDialHost(QAbstractHost *parent):
    QwtAbstractSliderHost(parent)
{
}

void QwtDialHost::init_property()
{
    QwtAbstractSliderHost::init_property();

    QAbstractProperty* pro;

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","lineWidth");
        pro->set_attribute("show_name",tr("Frame Width"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","frameShadow");
        pro->set_attribute("show_name",tr("Frame Shadow"));
        pro->set_attribute("group","Attributes");

        ComboItems   items;
        tagComboItem item;

        item.m_text="Plain";
        item.m_value=QFrame::Plain;
        items.append(item);

        item.m_text="Raised";
        item.m_value=QFrame::Raised;
        items.append(item);

        item.m_text="Sunken";
        item.m_value=QFrame::Sunken;
        items.append(item);

        pro->set_attribute("items",QVariant::fromValue<ComboItems>(items));
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","mode");
        pro->set_attribute("show_name",tr("Mode"));
        pro->set_attribute("group","Attributes");

        ComboItems   items;
        tagComboItem item;

        item.m_text="Rotate Needle";
        item.m_value=QwtDial::RotateNeedle;
        items.append(item);

        item.m_text="Rotate Scale";
        item.m_value=QwtDial::RotateScale;
        items.append(item);

        pro->set_attribute("items",QVariant::fromValue<ComboItems>(items));
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Float");
    if(pro!=NULL)
    {
        pro->set_property("name","origin");
        pro->set_attribute("show_name",tr("Origin"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Float");
    if(pro!=NULL)
    {
        pro->set_property("name","minScaleArc");
        pro->set_attribute("show_name",tr("Min Scale Arc"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Float");
    if(pro!=NULL)
    {
        pro->set_property("name","maxScaleArc");
        pro->set_attribute("show_name",tr("Max Scale Arc"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","frame_color");
        pro->set_attribute("show_name",tr("Frame Color"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        pro->setProperty("color",true);
        m_object->setProperty("frame_color",pro->get_value());
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","background_color");
        pro->set_attribute("show_name",tr("Background Color"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        m_object->setProperty("frame_color",pro->get_value());
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","contentsColor");
        pro->set_attribute("show_name",tr("Contents Color"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        QLinearGradient g;
        g.setColorAt(0,QColor(0,0,0,0));
        m_object->setProperty("contentsColor",QVariant::fromValue<QGradient>(g));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","text_color");
        pro->set_attribute("show_name",tr("Text Color"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        pro->setProperty("color",true);
        m_object->setProperty("textColor",pro->get_value());
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Clock_Needle");
    if(pro!=NULL)
    {
        pro->set_property("name","needle_info");
        pro->set_attribute("show_name",tr("Needle"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,200,200));
}

QString QwtDialHost::get_show_name()
{
    return tr("QWT Dial");
}

QString QwtDialHost::get_show_group()
{
    return tr("QWT Widgets");
}

QString QwtDialHost::get_show_icon()
{
    return ":/images/dial.png";
}

void QwtDialHost::create_object()
{
    m_object=new QDialWidget;
    m_object->setObjectName("dial");
}

qRegisterHost("qwt.dial",QwtDialHost,6010)
